/*
* @Author: Someu
* @Date:   2017-11-28 15:00:51
* @Last Modified by:   Someu
* @Last Modified time: 2017-12-04 10:17:51
*/

var webpack             = require('webpack');
var ExtractTextPlugin   = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin   = require("html-webpack-plugin");

//环境变量配置,dev/online
var WEBPACK_ENV         = process.env.WEBPACK_ENV || 'dev';

//获取html-webpack-plugin参数
var getHtmlConfig = function(name, title){
    return {
        template    : './src/view/'+name+'.html',
        filename    : 'view/'+name+'.html',
        title       : title,
        inject      : true,
        hash        : true,
        chunks      : ['common', name]
    }

}

//webpack config
var config = {
    entry: {
        'common'                : ['./src/page/common/index.js'],
        'user-login'            : ['./src/page/user-login/index.js'],
        'index'                 : ['./src/page/index/index.js'],
        'result'                : ['./src/page/result/index.js'],
        'user-register'         : ['./src/page/user-register/index.js'],
        'user-password-reset'   : ['./src/page/user-password-reset/index.js'],
        'user-center'           : ['./src/page/user-center/index.js'],
        'user-center-update'    : ['./src/page/user-center-update/index.js'],
        'user-password-update'    : ['./src/page/user-password-update/index.js']
    },
    output: {
        path: './dist', /*dist*/
        publicPath: '/dist/', /*html中*/
        filename: 'js/[name].js'
    },
    externals: {
        'jquery' : 'window.jquery',
    },
    resolve: {
        alias : {
            node_modules    : __dirname + '/node_modules',
            util            : __dirname + '/src/util',
            page            : __dirname + '/src/page',
            service         : __dirname + '/src/service',
            image           : __dirname + '/src/image'
        }
    },
    plugins: [
        //独立通用模块
        new webpack.optimize.CommonsChunkPlugin({
            name : 'common', /*如果name为entry中值，设置全局*/
            filename : 'js/base.js' /*基于output的puth*/
        }),
        //css单独打包到每个文件
        new ExtractTextPlugin("css/[name].css"),
        //html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig('index','首页')),
        new HtmlWebpackPlugin(getHtmlConfig('user-login','用户登录')),
        new HtmlWebpackPlugin(getHtmlConfig('result','操作结果')),
        new HtmlWebpackPlugin(getHtmlConfig('user-register','用户注册')),
        new HtmlWebpackPlugin(getHtmlConfig('user-password-reset','重置密码')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center','个人中心')),
        new HtmlWebpackPlugin(getHtmlConfig('user-center-update','修改个人信息')),
        new HtmlWebpackPlugin(getHtmlConfig('user-password-update','修改密码'))
    ],
    module: {
        loaders: [
            {test:/\.css$/, loader: ExtractTextPlugin.extract("style-loader","css-loader")},
            {test:/\.(gif|png|jpg|woff|svg|eot|ttf|ico)\??.*$/, loader: 'url-loader?limit=1024&name=[name].[ext]&outputPath=img/'},
            {test:/\.string$/, loader: 'html-loader'}
        ]
    }

};
module.exports = config;

if('dev' == WEBPACK_ENV){
    config.entry.common.push('webpack-dev-server/client?http://localhost:8080/');
}